--
-- Author:      feilong
-- DateTime:    2019-11-15 15:46:15
-- Description: 服务入口

local game_name = "erbagang" --目录名
local skynet = require("skynet")

local Hotfix = require("Hotfix")
local MessageDispatch = import("MessageDispatch") --使用import 的才可以热更
local UserManager = import("UserManager")
local DeskMessage = import("DeskMessage")
local RoomConfig = import("RoomConfig")
local RobotManager = import("RobotManager")
local StoreManager = import("StoreManager")
local BankerManager = import("BankerManager")
local BaiRenDesk = import("BaiRenDesk")

local MessageHandler = import(game_name..".MessageHandler")
local Desk = import(game_name..".Desk")
local Rule = import(game_name..".Rule")



--此服务唯一全局变量, 
global = {}

--禁止出现别的全局变量
local function disable_global()
	local __g = _G
	setmetatable(global, {
	    __newindex = function(_, name, value)
	        rawset(__g, name, value)
	    end,
	    __index = function(_, name)
	        return rawget(__g, name)
	    end
	})
	--防止手误写出全局变量
	setmetatable(__g, {
	    __newindex = function(_, name, value)
	        local msg = "USE 'global.%s = value' INSTEAD OF SET GLOBAL VARIABLE"
	        error(string.format(msg, name), 0)
	    end,
	})
end

--创建相关对象
local function init()
	local hotfix = Hotfix.new()
	hotfix:start()
	global.hotfix = hotfix

	local message_dispatch = MessageDispatch.new()	
	local rule = Rule.new()
	local desk = Desk.new()
	local desk_message = DeskMessage.new(message_dispatch)
	local message_handler = MessageHandler.new(message_dispatch)
	local room_config = RoomConfig.new()
	local user_manager = UserManager.new(room_config)
	local robot_manager = RobotManager.new(message_dispatch)
	local store_manager = StoreManager.new(message_dispatch)
	local banker_manager = BankerManager.new(message_dispatch)
	local bairen_desk = BaiRenDesk.new(message_dispatch)

	global.game_name = game_name
	global.message_dispatch = message_dispatch	
	global.rule = rule
	global.desk = desk
	global.user_manager = user_manager
	global.desk_message = desk_message
	global.room_config = room_config
	global.message_handler = message_handler
	global.robot_manager = robot_manager
	global.store_manager = store_manager
	global.banker_manager = banker_manager
	global.bairen_desk = bairen_desk
	
	--
	skynet.register_protocol({
		name = "client",
		id = skynet.PTYPE_CLIENT,
		pack = skynet.pack,
		unpack = skynet.unpack,
	})
	
	--消息派发
	local func = message_dispatch:dispatchClient()	
	skynet.dispatch("client", func)
	skynet.dispatch("lua", message_dispatch:dispatchLua())
	dbglog("_______游戏服启动________________")
end

---------------------------------------------------------
-- skynet service start
---------------------------------------------------------
disable_global()
init()
skynet.start(function() end)

